<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/4 0004
 * Time: 09:30
 */

namespace app\admin\controller;


use think\Db;

class SiteConfig extends Base
{
    public $table = 'site_config';
    public $client;
    public $banner_table = 'site_banner';
    public $title;

    public function banner()
    {
        $data['list'] = Db::name($this->banner_table)->where('site_id',$this->site_id)->where('client',$this->client)->order('id desc')->paginate();
        $data['page'] = $data['list']->render();
        $this->assign('data',$data);
        $this->assign('title',$this->title);
        return $this->fetch('site_config/banner');
    }

    public function add_banner()
    {

        if(request()->isPost()){
            return $this->save_banner();
        }

        $this->assign('data',1);
        $this->assign('title','新增轮播图');
        $this->assign('url',url(__FUNCTION__));
        return $this->fetch('site_config/banner_info');
    }

    public function update_banner()
    {
        $id = input('id/d');

        $data = Db::name($this->banner_table)->where('id',$id)->find();
        if(!$data){
            $this->error('不存在');
        }

        if(request()->isPost()){
            return $this->save_banner();
        }
        $data['password'] = '';

        $this->assign('data',json_encode($data));
        $this->assign('title','编辑轮播图');
        $this->assign('url',url(__FUNCTION__));
        return $this->fetch('site_config/banner_info');
    }

    public function save_banner()
    {
        $id = input('id/d');
        $param = input();
        $data = [
            'img'=>input('img'),
            'title'=>input('title'),
            'link'=>input('link'),
            'display'=>input('display'),
            'sort'=>input('sort'),
            'modtime'=>time(),
        ];

        if($id){
            //编辑
            $a = Db::name($this->banner_table)->where('id',$id)->update($data);
        }else{
            //新增
            $data['client'] = $this->client;
            $data['site_id'] = $this->site_id;

            $data['addtime'] = time();

            $a = Db::name($this->banner_table)->insert($data);
        }
        if($a){
            echo api_return(1,'保存成功');
        }else{
            echo api_return(0,'保存失败');
        }
        return;
    }

    public function ajax_update_banner()
    {
        $field = input('field');
        $data[$field] = input('value/d');
        $id = input('id/d');
        $data['modtime'] = time();

        $a = Db::name($this->banner_table)->where('id',$id)->update($data);

        if($a){
            echo api_return(1,'保存成功');
        }else{
            echo api_return(0,'保存失败');
        }
    }

    public function top()
    {
        return $this->base_term(1);
    }

    public function body()
    {
        return $this->base_term(2);
    }

    public function base_term($position)
    {
        $data = Db::name('term')->where('site_id',$this->site_id)->where('level',1)->order('id asc')->select();
        $config = Db::name('term_config')->where('site_id',$this->site_id)->where('client',$this->client)->where('position',$position)->select();
        foreach ($data as $key=>$value){
            $data[$key]['display'] = 0;
            if($config){
                foreach ($config as $v){
                    if($v['term_id'] == $value['id']){
                        $data[$key]['display'] = $v['display'];
                        $data[$key]['sort'] = $v['sort'];
                        break;
                    }
                }
            }
        }
        $url = '1';
        if($position == 1){
            $url = url('ajax_update_top');
        }elseif ($position == 2){
            $url = url('ajax_update_body');
        }
        $this->assign('title',$this->title.'顶部导航配置');
        $this->assign('data',$data);
        $this->assign('url',$url);
        return $this->fetch('site_config/term');
    }

    public function ajax_update_top()
    {
        return $this->base_update_term($this->client,1);
    }

    public function ajax_update_body()
    {
        return $this->base_update_term($this->client,2);
    }

    public function base_update_term($client,$position)
    {
        $field = input('field');
        $value = input('value/d');
        $id = input('id/d');
        $config = Db::name('term_config')
            ->where('site_id',$this->site_id)
            ->where('term_id',$id)
            ->where('client',$client)
            ->where('position',$position)
            ->find();
        $model['modtime'] = time();
        if($field == 'display'){
            $model['display'] = $value;
        }
        if($field == 'sort'){
            $model['sort'] = $value;
        }
        if($config){

            $res = Db::name('term_config')->where('id',$config['id'])->update($model);
        }else{
            $model['site_id'] = $this->site_id;
            $model['term_id'] = $id;
            $model['client'] = $client;
            $model['position'] = $position;
            $model['sort'] = $model['sort']??0;
            $model['display'] = $model['display']??0;
            $model['addtime'] = time();
            $res = Db::name('term_config')->insert($model);
        }

        if($res){
            return dataReturn(1,'保存成功');
        }else{
            return dataReturn(0,'保存失败');
        }
    }


    private function saveConfig()
    {
        $config = Db::name(self::TABLE_CONFIG)->where('status',1)->select();
        foreach ($config as $v)
        {
            $data[$v['name']] = $v['value'];
        }
        //生成配置文件
        $config_file = APP_PATH . 'extra/setting.php';
        create_file ( $config_file );
        arr2file ( $config_file, $data );
    }
}